<link href="asset/css/show.css" rel="stylesheet" type="text/css">
<script src="asset/js/cetak_excel.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#show").hide();
	$("#sembunyi").hide();
	$("#tampil").show();
	
	$("#tampil").click(function(){
		$("#show").show(500);
		$("#tampil").hide();
		$("#sembunyi").show();
	});
	$("#sembunyi").click(function(){
		$("#show").hide(500);
		$("#sembunyi").hide();
		$("#tampil").show();
	});
	
	$("#periode").change(function(){
		var periode = $("#periode").val();
		if(periode == 1){
			document.getElementById('tgl1').disabled = false;
			document.getElementById('tgl2').disabled = false;
			document.getElementById('bln1').disabled = false;
			document.getElementById('bln2').disabled = false;
		}
		else if(periode == 2){
			document.getElementById('tgl1').disabled = true;
			document.getElementById('tgl2').disabled = true;
			document.getElementById('bln1').disabled = false;
			document.getElementById('bln2').disabled = false;
		}else{
			document.getElementById('tgl1').disabled = true;
			document.getElementById('tgl2').disabled = true;
			document.getElementById('bln1').disabled = true;
			document.getElementById('bln2').disabled = true;
		}
	});
});
</script>
<?php
	$tgl = date("d");
	$bln = date("m");
	$thn = date("Y");
	
	$periode = $_POST['periode'];
	$tgl1 = $_POST['tgl1'];
	$tgl2 = $_POST['tgl2'];
	$bln1 = $_POST['bln1'];
	$bln2 = $_POST['bln2'];
	$thn1 = $_POST['thn1'];
	$thn2 = $_POST['thn2'];
	$wlyh = $_POST['wilayah'];
?>
<div id="tiga_ba">
	<div id="tiga_baa">Prosentase Peserta </div>
	<div id="tiga_bab">
	
	</div>
	<div class="clear"></div>
</div>

<form method=post action='utama.php?module=mod_grafik&view=peserta'>
	<table style="margin-left: 5px;">
		<tr>
			<td width="75">Periode </td>
			<td width="300">
				<select name="periode" id="periode" class="input1">
					<?php
						if($periode == 1){
							$s1 = "selected";
						}else if($periode == 2){
							$p1 = "disabled";
							$s2 = "selected";
						}else{
							$p1 = "disabled";
							$p2 = "disabled";
							$s3 = "selected";
						}
					?>
					<option value="1" <?php echo $s1; ?>>Hari</option>
					<option value="2" <?php echo $s2; ?>>Bulan</option>
					<option value="3" <?php echo $s3; ?>>Tahun</option>
				</select>
			</td>
		</tr>
		<tr>
			<td>Dari </td>
			<td>
				<select name="tgl1" id="tgl1" class="input1" <?php echo $p1; ?>>
					<option value="">Tgl</option>
					<?php
						for($i=1;$i<=31;$i++){
							if($i == $tgl && $tgl1 == ''){
								$s = "selected";
							}else if($i == $tgl1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="bln1" id="bln1" class="input1" <?php echo $p2; ?>>
					<option value="">Bln</option>
					<?php
						for($i=1;$i<=12;$i++){
							if($i == $bln && $bln1 == ''){
								$s = "selected";
							}else if($i == $bln1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="thn1" id="thn1" class="input1">
					<option value="">Thn</option>
					<?php
						for($i=2010;$i<=2020;$i++){
							if($i == $thn && $thn1 == ''){
								$s = "selected";
							}else if($i == $thn1){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Sampai </td>
			<td>
				<select name="tgl2" id="tgl2" class="input1" <?php echo $p1; ?>>
					<option value="">Tgl</option>
					<?php
						for($i=1;$i<=31;$i++){
							if($i == $tgl && $tgl2 == ''){
								$s = "selected";
							}else if($i == $tgl2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="bln2" id="bln2" class="input1" <?php echo $p2; ?>>
					<option value="">Bln</option>
					<?php
						for($i=1;$i<=12;$i++){
							if($i == $bln && $bln2 == ''){
								$s = "selected";
							}else if($i == $bln2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
				<select name="thn2" id="thn2" class="input1">
					<option value="">Thn</option>
					<?php
						for($i=2010;$i<=2020;$i++){
							if($i == $thn && $thn2 == ''){
								$s = "selected";
							}else if($i == $thn2){
								$s = "selected";
							}else{
								$s = "";
							}
							echo "<option value=$i $s>$i</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Wilayah</td>
			<td>
				<select name="wilayah" id="wilayah" class="input2">
					<option value=""> - - Pilih Semua - - </option>
					<?php
						$wilayah = mysql_query("select * from kecamatan k inner join kabupaten kb on k.id_kabupaten = kb.id_kabupaten inner join propinsi p on p.id_propinsi = kb.id_propinsi where p.aktif = 'Y' and kb.aktif = 'Y' order by kecamatan");
						while($w = mysql_fetch_array($wilayah)){
							if($wlyh == $w['id_kecamatan']){
								$sw = "selected";
							}else{
								$sw = "";
							}
							echo "<option value=$w[id_kecamatan] $sw>$w[kecamatan]</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td></td>
			<td><input name="submit" id="proses" type="submit" class="button" value="Proses" /></td>
			<td></td>
			<td></td>
		</tr>
	</table>
</form>
<?php
	if($periode == 1){
		$tgl1 = "and p.tgl_mulai >= '$thn1-$bln1-$tgl1'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-$bln2-$tgl2'";
	}else if($periode == 2){
		$tgl1 = "and p.tgl_mulai >= '$thn1-$bln1-01'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-$bln2-31'";
	}else{
		$tgl1 = "and p.tgl_mulai >= '$thn1-01-01'";
		$tgl2 = "and p.tgl_mulai <= '$thn2-12-31'";
	}
	
	if($wlyh == ''){
		$sql1=mysql_query("select count(id_peserta) as jml from peserta p inner join penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk INNER JOIN desa d ON k.id_desa = d.id_desa inner join kecamatan kc on kc.id_kecamatan = d.id_kecamatan inner join kabupaten kb on kb.id_kabupaten = kc.id_kabupaten where kb.aktif = 'Y'");
		$sql2=mysql_query("select count(id_penduduk) as jml from penduduk pn INNER JOIN keluarga k ON k.id_kk = pn.id_kk INNER JOIN desa d ON k.id_desa = d.id_desa inner join kecamatan kc on kc.id_kecamatan = d.id_kecamatan inner join kabupaten kb on kb.id_kabupaten = kc.id_kabupaten where kb.aktif = 'Y'");
	}else{
		$sql1=mysql_query("select count(id_peserta) as jml from peserta p inner join penduduk pn ON p.id_penduduk = pn.id_penduduk INNER JOIN keluarga k ON k.id_kk = pn.id_kk INNER JOIN desa d ON k.id_desa = d.id_desa inner join kecamatan kc on kc.id_kecamatan = d.id_kecamatan inner join kabupaten kb on kb.id_kabupaten = kc.id_kabupaten where kb.aktif = 'Y' and kc.id_kecamatan = '$wlyh'");
		$sql2=mysql_query("select count(id_penduduk) as jml from penduduk pn INNER JOIN keluarga k ON k.id_kk = pn.id_kk INNER JOIN desa d ON k.id_desa = d.id_desa inner join kecamatan kc on kc.id_kecamatan = d.id_kecamatan inner join kabupaten kb on kb.id_kabupaten = kc.id_kabupaten where kb.aktif = 'Y' and kc.id_kecamatan = '$wlyh'");
	}
	
	if($_POST['submit'] == 'Proses'){
		$jml_peserta = mysql_fetch_array($sql1);
		$jml_penduduk = mysql_fetch_array($sql2);
		$jml1 = $jml_peserta['jml'];
		$jml2 = $jml_penduduk['jml'];
		$persen1 = desimal( $jml1/$jml2 * 100);
		$persen2 = desimal(($jml2 - $jml1) / $jml2 * 100);
	?>
		<script type="text/javascript" src="asset/js/jquery.fusioncharts.js"></script>
		<div id="btnChart">
			<span id="tampil"><img src="asset/images/chart.png" width="30"></span>
			<span id="sembunyi"><img src="asset/images/chart2.png" width="30"></span>
			<span id="print"><img src="asset/images/print.png" width="30"></span>
		</div>
		<div id="show">
			<script type="text/javascript">
				$(function () {
					$('#container').highcharts({
						chart: {
							plotBackgroundColor: null,
							plotBorderWidth: null,
							plotShadow: false
						},
						title: {
							text: 'Prosentase Jumlah Peserta Dibanding Jumlah Penduduk'
						},
						tooltip: {
							pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
						},
						plotOptions: {
							pie: {
								allowPointSelect: true,
								cursor: 'pointer',
								dataLabels: {
									enabled: true,
									color: '#000000',
									connectorColor: '#000000',
									format: '<b>{point.name}</b>: {point.percentage:.1f} %'
								}
							}
						},
						series: [{
							type: 'pie',
							name: 'Jumlah ',
							data: [
								['Peserta ', <?php echo $persen1 ?>],
								['Bukan Peserta ', <?php echo $persen2 ?>]
							]
						}]
					});
				});
			</script>
			<script src="asset/js/highcharts.js"></script>
			<script src="asset/js/modules/exporting.js"></script>
			<div id="container" style="width: 500px; height: 400px; margin: auto"></div>
		</div>
		<!------------------------------------------------------------------->
		<div id="dvData">
		<table class="tabel">
			<tr>
				<th width="40">NO</th>
				<th>Keterangan</th>
				<th>Jumlah</th>
				<th width="120">Persentase (%)</th>
			</tr>
			<tr>
				<td align="center">1</td>
				<td>Peserta</td>
				<td align="center"><?php echo $jml_peserta['jml']; ?></td>
				<td align="center"><?php echo $persen1 ?></td>
			</tr>
			<tr>
				<td align="center">2</td>
				<td>Bukan Peserta</td>
				<td align="center">
					<?php 
						$non_peserta = $jml_penduduk['jml'] - $jml_peserta['jml'];
						echo $non_peserta; 
					?>
				</td>
				<td align="center"><?php echo $persen2 ?></td>
			</tr>
			<tr>
				<td align="center" colspan="2"><b>Jumlah</b></td>
				<td align="center" align="center" width="60">
					<?php
						echo $jml_penduduk['jml'];
					?>
				</td>
				<td align="center" align="center" width="60">
					<?php
						$persen = $persen1 + $persen2;
						echo desimal("$persen");
					?>
				</td>
			</tr>
		</table>
		</div>
	<?php
	}
	?>
